package com.photoeditor.demo.model;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.common.base.utils.DebugUtil;
import com.photoeditor.demo.app.TestUser;
import com.photoeditor.demo.db.FilterCategoryBean;
import com.photoeditor.demo.model.bean.EditEmojiBean;
import com.photoeditor.demo.model.bean.EmojiBean;
import com.photoeditor.demo.model.store.module.BaseOnlineManager;
import com.photoeditor.demo.model.bean.stash.ContainerBean;

import java.util.ArrayList;

/**
 * Created by mutour on 2017/10/30.
 */

public class UseEffectInfo implements Parcelable {

    private boolean DEBUG = false;
    private ArrayList<EffectBean> mEffectBeans = new ArrayList<>();

    public UseEffectInfo() {
        DEBUG = DebugUtil.isDebuggable() || TestUser.isTestUser();
    }

    protected UseEffectInfo(Parcel in) {
        DEBUG = in.readByte() != 0;
        mEffectBeans = in.createTypedArrayList(EffectBean.CREATOR);
    }

    public static final Creator<UseEffectInfo> CREATOR = new Creator<UseEffectInfo>() {
        @Override
        public UseEffectInfo createFromParcel(Parcel in) {
            return new UseEffectInfo(in);
        }

        @Override
        public UseEffectInfo[] newArray(int size) {
            return new UseEffectInfo[size];
        }
    };

    public ArrayList<EffectBean> getEffectBeans() {
        return mEffectBeans;
    }

    public synchronized void add(ContainerBean bean) {
        if (bean instanceof EditEmojiBean) {
            add((EditEmojiBean) bean);
        }
    }

    public synchronized void add(EditEmojiBean bean) {
        if (bean == null) {
            return;
        }
        EmojiBean emojiBean = bean.getEmojiBean();
        add(emojiBean);
    }

    public synchronized void add(EmojiBean bean) {
        if (bean == null) {
            return;
        }
//        String packageName = bean.getPackageName();
//        String effectType = BaseOnlineManager.STORE_FUNC_KEY_STICKER;
//        String effectName = bean.getCategoryName();
//        boolean downloadOnGP = bean.isDownloadOnGP();
//        mEffectBeans.add(new EffectBean(packageName, effectName, effectType, downloadOnGP));
    }

    public synchronized void add(FilterCategoryBean bean) {
        if (bean == null) {
            return;
        }
        String packageName = bean.getPkgName();
        String effectType = BaseOnlineManager.STORE_FUNC_KEY_FILTER;
        String effectName = bean.getName();
        boolean downloadOnGP = bean.isDownloadOnGP();
        mEffectBeans.add(new EffectBean(packageName, effectName, effectType, downloadOnGP));
    }

    public void remove(ContainerBean bean) {
        if (bean instanceof EditEmojiBean) {
            remove((EditEmojiBean) bean);
        }
    }

    public void remove(EditEmojiBean bean) {
        if (bean != null) {
            remove(bean.getPkgName());
        }
    }

    public void remove(FilterCategoryBean bean) {
        if (bean != null) {
            remove(bean.getPkgName());
        }
    }

    public void remove(String packageName) {
        if (!TextUtils.isEmpty(packageName)) {
            for (int i = 0; i < mEffectBeans.size(); i++) {
                EffectBean effectBean = mEffectBeans.get(i);
                if (TextUtils.equals(effectBean.packageName, packageName)) {
                    mEffectBeans.remove(i);
                    return;
                }
            }
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByte((byte) (DEBUG ? 1 : 0));
        dest.writeTypedList(mEffectBeans);
    }


    public static class EffectBean implements Parcelable {
        public String packageName = "";
        public String type = "";
        public String name = "";
        public boolean isDownloadGP = false;

        public EffectBean(String packageName, String name, String type, boolean isDownloadGP) {
            this.packageName = packageName;
            this.type = type;
            this.name = name;
            this.isDownloadGP = isDownloadGP;
        }

        protected EffectBean(Parcel in) {
            packageName = in.readString();
            type = in.readString();
            name = in.readString();
            isDownloadGP = in.readByte() != 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(packageName);
            dest.writeString(type);
            dest.writeString(name);
            dest.writeByte((byte) (isDownloadGP ? 1 : 0));
        }

        @Override
        public int describeContents() {
            return 0;
        }

        public static final Creator<EffectBean> CREATOR = new Creator<EffectBean>() {
            @Override
            public EffectBean createFromParcel(Parcel in) {
                return new EffectBean(in);
            }

            @Override
            public EffectBean[] newArray(int size) {
                return new EffectBean[size];
            }
        };

        @Override
        public String toString() {
            return "EffectBean{" +
                    "packageName='" + packageName + '\'' +
                    ", type='" + type + '\'' +
                    ", name='" + name + '\'' +
                    ", isDownloadGP=" + isDownloadGP +
                    '}';
        }
    }
}
